package com.whsr.readservice.service;

import com.whsr.readservice.dao.PageConfigGroupMapper;
import com.whsr.readservice.dao.PageConfigMapper;
import com.whsr.readservice.vo.PageConfig;
import com.whsr.readservice.vo.PageConfigGroup;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("pageConfigService")
@Transactional
public class PageConfigService {


    private final PageConfigGroupMapper pageConfigGroupMapper;
    private final PageConfigMapper pageConfigMapper;

    public PageConfigService(PageConfigGroupMapper pageConfigGroupMapper, PageConfigMapper pageConfigMapper) {
        this.pageConfigGroupMapper = pageConfigGroupMapper;
        this.pageConfigMapper = pageConfigMapper;
    }


    /**
     * @author chatwin
     * 插入配置分类信息
     * @param configGroup
     * @return
     */
    public int addConfigGroup(PageConfigGroup configGroup) {
        int result = pageConfigGroupMapper.add(configGroup);
        return result;
    }

    /**
     * @author chatwin
     * 更新配置分类信息
     * @param configGroup
     * @return
     */
    public int updateConfigGroup(PageConfigGroup configGroup) {
        int result = pageConfigGroupMapper.update(configGroup);
        return result;
    }

    /**
     * @author chatwin
     * 删除首页分类信息，可能存在子分类或者已经配置过书籍信息的，一并删除
     * @param id
     * @return
     */
    public int removeConfigGroup(int id) {
        //int result = pageConfigMapper.removeConfig(id);
        int result = pageConfigMapper.removeConfigByParentId(id);
        if(result < 0)
            throw new RuntimeException("REMOVE PAGECONFIG ERR");

        result = pageConfigGroupMapper.removeChildren(id);
        if(result < 0)
            throw new RuntimeException("REMOVE CHILDREN GROUP ERR");

        result = pageConfigGroupMapper.remove(id);
        return result;
    }

    public List<PageConfigGroup> queryConfigGroup(Object name) {
        List<PageConfigGroup> list = pageConfigGroupMapper.queryConfigGroup(name);
        return list;
    }

    public PageConfigGroup getConfigGroupById(int id) {
        return this.pageConfigGroupMapper.getConfigGroupById(id);
    }

    public List getConfigList(Object name) {
        List list = pageConfigMapper.queryConfigList(name);
        return list;
    }

    public PageConfig getConfigById(int id) {
        return this.pageConfigMapper.getConfigById(id);
    }


    public int updateConfig(PageConfig pageConfig) {
        int result = pageConfigMapper.add(pageConfig);
        return result;
    }

    public int addConfig(PageConfig pageConfig) {
        int result = pageConfigMapper.update(pageConfig);
        return result;
    }

    public List<PageConfigGroup> getConfigGroupByParentId(int parentId) {
        return this.pageConfigGroupMapper.getConfigGroupByParentId(parentId);
    }

    public int removeConfig(int id) {
        return this.pageConfigMapper.removeConfig(id);
    }

    public List<Map<String,Object>> getIndexTab(String type) {
        return this.pageConfigMapper.getIndexTab(type);
    }

    public List<Map<String,Object>> getMobileIndex(int id) {
        return this.pageConfigMapper.getMobileIndex(id);
    }

    public List<Map<String, Object>> getChildren(int id, String templet){
        if("carousel".equals(templet) || "categoryCard".equals(templet)){
            return this.pageConfigMapper.getArtChildren(id);
        }else if("buttonone".equals(templet) || "buttonTwo".equals(templet)){
            return this.pageConfigMapper.getTypeChildren(id);
        }else{
            return this.pageConfigMapper.getButtonChildren(id);
        }
    }

    public Map<String,Object> getArtByPage(Map<String, Object> resMap) {
        int pageIndex = resMap.get("pageIndex") == null ? -1 : Integer.valueOf(resMap.get("pageIndex").toString());
        int pageSize = resMap.get("pageSize") == null ? 20 : Integer.valueOf(resMap.get("pageSize").toString());
        int categoryId = resMap.get("categoryId") == null ? -1 : Integer.valueOf(resMap.get("categoryId").toString());
        String key  = resMap.get("key") == null ? null : resMap.get("key").toString();
        int startIndex = pageIndex == -1 ? -1 : (pageIndex -1) * pageSize;

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("total", this.pageConfigGroupMapper.getCountArtByPage(categoryId, key));
        resultMap.put("data", this.pageConfigGroupMapper.getArtByPage(startIndex, pageSize, categoryId, key));
        return resultMap;
    }
}
